package com.iot.ic.service;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iot.ic.common.CustomException;
import com.iot.ic.common.Options;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.MatchScenesInfoMapper;
import com.iot.ic.dao.RefereeInfoMapper;
import com.iot.ic.dao.TeamInfoMapper;
import com.iot.ic.model.EquipmentInfo;
import com.iot.ic.model.IllegalInfo;
import com.iot.ic.model.MatchScenesInfo;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.ScenesResult;
import com.iot.ic.model.StadiumResult;
import com.iot.ic.model.StadiumScenes;
import com.iot.ic.model.TeamInfo;

/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class MatchScenesServiceImpl implements MatchScenesService {

	@Autowired
	private MatchScenesInfoMapper matchScenesInfoMapper;
	
	@Autowired
	private IllegalService illegalService;
	
	@Autowired
	private EquipmentService equipmentService;
	
	@Autowired
	private ScenesResultService scenesResultService;
	
	@Autowired
	private StadiumScenesService stadiumScenesService;
	
	@Autowired
	private StadiumResultService stadiumResultService;

	@Override
	public List<Integer> listMatchScenesIds(int stadiumId) {
		List<Integer> resultList =  new ArrayList<Integer>();
		List<MatchScenesInfo> matchScenesInfoList = matchScenesInfoMapper.getMatchScenesIdsByStadium(stadiumId);
		for (MatchScenesInfo matchScenesInfo : matchScenesInfoList) {
			resultList.add(matchScenesInfo.getScenesId());
		}
		return resultList;
	}

	@Override
	public MatchScenesInfo getMatchScenesInfo(int scenesId) {
		MatchScenesInfo matchScenesInfo = matchScenesInfoMapper.selectByPrimaryKey(scenesId);
		List<IllegalInfo> illegalInfoList = illegalService.listIllegalInfosByScenes(matchScenesInfo.getScenesId());
		List<EquipmentInfo> equipmentInfoList = equipmentService.listEquipmentInfosByScenes(matchScenesInfo.getScenesId());
		matchScenesInfo.setIllegalInfoList(illegalInfoList);
		matchScenesInfo.setEquipmentInfoList(equipmentInfoList);
		return matchScenesInfo;
	}
	
/*	@Override
	public MatchScenesInfo getMatchScenesInfo(int scenesId) {
		MatchScenesInfo matchScenesInfo = matchScenesInfoMapper.selectByPrimaryKey(scenesId);
		List<IllegalInfo> illegalInfoList = illegalService.listIllegalInfosByScenes(matchScenesInfo.getScenesId());
		matchScenesInfo.setIllegalInfoList(illegalInfoList);
		return matchScenesInfo;
	}*/

	@Override
	public MatchScenesInfo startMatchScenes(int stadiumScenesId, int stadiumResultId) {
		//根据场景ID获得场景对象
		MatchScenesInfo matchScenesInfo = matchScenesInfoMapper.getMatchScenesByStadiumScenesId(stadiumScenesId);
		if (matchScenesInfo == null) {
			throw new CustomException(ReturnCode.Err_NoScenes.getDescription());
		}
		//根据场景Id获得违规行为和设备集合
		List<IllegalInfo> illegalInfoList = illegalService.listIllegalInfosByScenes(matchScenesInfo.getScenesId());
		List<EquipmentInfo> equipmentInfoList = equipmentService.listEquipmentInfosByScenes(matchScenesInfo.getScenesId());
		matchScenesInfo.setIllegalInfoList(illegalInfoList);
		matchScenesInfo.setEquipmentInfoList(equipmentInfoList);
		ScenesResult oldScenesResult = scenesResultService.getScenesResultByScenesAndStadiumResult(stadiumScenesId, stadiumResultId);
		if (oldScenesResult == null) {
			ScenesResult scenesResult = new ScenesResult();
			scenesResult.setCreateTime(new Date());
			scenesResult.setScenesResult(matchScenesInfo.getScenesScore());
			scenesResult.setStadiumResultId(stadiumResultId);
			scenesResult.setScenesId(matchScenesInfo.getScenesId());
			scenesResult.setStadiumScenesId(stadiumScenesId);
			scenesResult.setStadiumId(matchScenesInfo.getStadiumId());
			scenesResult.setScenesName(matchScenesInfo.getScenesName());
			scenesResult.setManualInterventionFlag(0);
			scenesResult.setDelFlag(Options.Deleted.getCode());
			scenesResult.setScenesType(matchScenesInfo.getScenesType());
			scenesResult.setScenesNo(matchScenesInfo.getScenesNo());
			scenesResultService.addScenesResult(scenesResult);
			int scenesResultId = scenesResult.getScenesResultId();
			matchScenesInfo.setScenesResultId(scenesResultId);
		} else {
			matchScenesInfo.setScenesResultId(oldScenesResult.getScenesResultId());
		}

		
		return matchScenesInfo;
	}

	@Override
	public List<MatchScenesInfo> listScenesByStadium(int stadiumId) {
		List<MatchScenesInfo> matchScenesInfoList = matchScenesInfoMapper.listMatchScenesByStadium(stadiumId);
		return matchScenesInfoList;
	}

	@Override
	public int addMatchScenes(MatchScenesInfo matchScenes) {
		int result = matchScenesInfoMapper.insert(matchScenes);
		return result;
	}

	@Override
	public MatchScenesInfo getMatchScenesById(int scenesId) {
		MatchScenesInfo scenesInfo = matchScenesInfoMapper.selectByPrimaryKey(scenesId);
		return scenesInfo;
	}

	@Override
	public int updateMatchScenes(MatchScenesInfo matchScenes) {
		int result = matchScenesInfoMapper.updateByPrimaryKeySelective(matchScenes);
		return result;
	}

	@Override
	public int deleteMatchScenes(int scenesId) {
		List<StadiumScenes> stadiumScenesList = stadiumScenesService.listStadiumScenesByScenesId(scenesId);
		if (stadiumScenesList != null && stadiumScenesList.size() > 0) {
			throw new CustomException(ReturnCode.Err_ScenesStadiunBindAlready);
		}
		int result = matchScenesInfoMapper.deleteMatchScenes(scenesId);
		List<IllegalInfo> illegalInfoList = illegalService.listIllegalInfosByScenes(scenesId);
		for (IllegalInfo illegalInfo : illegalInfoList) {
			result = illegalService.deleteIllegalInfo(illegalInfo.getIllegalId());
		}
		List<EquipmentInfo> equipmentInfoList = equipmentService.listEquipmentInfosByScenes(scenesId);
		for(EquipmentInfo equipmentInfo : equipmentInfoList){
			result = equipmentService.deleteEquipmentInfo(equipmentInfo.getEquipmentId());
		}
		return result;
	}

	@Override
	public List<MatchScenesInfo> listUnboundScenesByStadium(int stadiumId) {
		List<MatchScenesInfo> matchScenesInfoList = matchScenesInfoMapper.selectUnboundScenesByStadium(stadiumId);
		return matchScenesInfoList;
	}

	@Override
	public List<MatchScenesInfo> listAllScenes(MatchScenesInfo matchScenes) {
		List<MatchScenesInfo> matchScenesInfoList = matchScenesInfoMapper.listAllScenes(matchScenes);
		return matchScenesInfoList;
	}

	@Override
	public List<MatchScenesInfo> pageScenes(MatchScenesInfo matchScenesInfo, Integer pageNo, Integer pageSize) {
		pageNo = pageNo < 1 ? 1:pageNo;
		int start = (pageNo - 1)*pageSize;
		return matchScenesInfoMapper.pageScenes(start, pageSize);
	}

	@Override
	public int pageTotal(MatchScenesInfo matchScenesInfo) {
		return matchScenesInfoMapper.pageTotal(matchScenesInfo);
	}

	@Override
	public int checkNameExist(MatchScenesInfo matchScenesInfo) {
//		TODO
		return matchScenesInfoMapper.checkNameExist(matchScenesInfo.getScenesName());
	}

	@Override
	public MatchScenesInfo getMatchScenesInfoWithIllegal(int scenesId) {
		MatchScenesInfo matchScenesInfo = matchScenesInfoMapper.selectByPrimaryKey(scenesId);
		List<IllegalInfo> illegalInfoList =  illegalService.listIllegalInfosByScenes(scenesId);
		matchScenesInfo.setIllegalInfoList(illegalInfoList);
		return matchScenesInfo;
	}
	
	@Override
	public MatchScenesInfo getMatchScenesInfoWithEquipment(int scenesId) {
		MatchScenesInfo matchScenesInfo = matchScenesInfoMapper.selectByPrimaryKey(scenesId);
		List<EquipmentInfo> equipmentInfoList = equipmentService.listEquipmentInfosByScenes(scenesId);
		matchScenesInfo.setEquipmentInfoList(equipmentInfoList);
		return matchScenesInfo;
	}

	@Override
	public List<MatchScenesInfo> listBoundScenesByStadium(int stadiumId) {
		List<MatchScenesInfo> matchScenesInfoList = matchScenesInfoMapper.selectBoundScenesByStadium(stadiumId);
		return matchScenesInfoList;
	}

	


	
	
	
	
	
}
